package com.audaque.springboot.foshanupload.web.amqpdemo.controller;

import cn.hutool.json.JSONUtil;
import com.audaque.springboot.foshanupload.web.amqpdemo.model.rsp.SysUserMpgtRsp;
import com.audaque.springboot.foshanupload.web.amqpdemo.service.IMessageServcie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;



/**
 * @author zgb
 * @desc ...
 * @date 2023-04-13 16:31:46
 */
@Slf4j
@RestController
@RequestMapping("/producerTemplate")
public class ProducerTemplateController {

  @Resource
  private RabbitTemplate rabbitTemplate;
  @Autowired
  private AmqpTemplate amqpTemplate;

  @Resource
  private CorrelationData correlationData;

  @Autowired
  private IMessageServcie messageServiceImpl;

  @RequestMapping("/sendMoreMsgTx")
  @ResponseBody
  public String sendMoreMsgTx(){
    //发送10条消息
    for (int i = 0; i < 10; i++) {
      String msg = "msg"+i;
      System.out.println("发送消息  msg："+msg);
      messageServiceImpl.sendMessage("directExchangeTx", "directQueueTxRoutingKey", msg);
      //每两秒发送一次
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    return "send ok";
  }

  @PostMapping("/rabbitTemplateSend")
  public void rabbitTemplateSend(@RequestParam(value = "exchange", required = false) String exchange,
                                 //路由键：队列的key
                                 @RequestParam(value = "routingKey", required = false) String routingKey,
                                 @RequestBody SysUserMpgtRsp sysUserMpgtRs) {
    /**
     * 通常情况下，我们会将一个 Java对象转换为一个 JSON 字符串，然后将该字符串作为消息发送给 RabbitMQ。
     * 在这个过程中，RabbitTemplate 会自动将 JSON 字符串加上一些消息头，以便 RabbitMQ 能够正确地理解和解析这个消息。
     */
    String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
    rabbitTemplate.convertAndSend(exchange, routingKey, jsonStr, correlationData);
  }
  @PostMapping("/rabbitTemplateSendDelayMessage")
  public void rabbitTemplateSendDelayMessage(
                                 //路由键：队列的key
                                 @RequestParam(value = "routingKey", required = false) String routingKey,
                                 @RequestBody SysUserMpgtRsp sysUserMpgtRs) {
    /**
     * 通常情况下，我们会将一个 Java对象转换为一个 JSON 字符串，然后将该字符串作为消息发送给 RabbitMQ。
     * 在这个过程中，RabbitTemplate 会自动将 JSON 字符串加上一些消息头，以便 RabbitMQ 能够正确地理解和解析这个消息。
     */
    String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
    //delay_exchange：已定义的交换器
    rabbitTemplate.convertAndSend("delay_exchange", routingKey, jsonStr, message -> {
      message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
      // 这里的时间用来设置延时多久
      message.getMessageProperties().setHeader("x-delay", 30000);
      return message;
    });

  }

  @PostMapping("/amqpTemplateSend")
  public void amqpTemplateSend(@RequestParam(value = "exchange", required = false) String exchange,
                               //路由键：队列的key
                               @RequestParam(value = "routingKey", required = false) String routingKey,
                               @RequestBody SysUserMpgtRsp sysUserMpgtRs) {
    /**
     * 通常情况下，我们会将一个 Java对象转换为一个 JSON 字符串，然后将该字符串作为消息发送给 RabbitMQ。
     * 在这个过程中，RabbitTemplate 会自动将 JSON 字符串加上一些消息头，以便 RabbitMQ 能够正确地理解和解析这个消息。
     */
    String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
    amqpTemplate.convertAndSend(exchange, routingKey, jsonStr);
  }

  @PostMapping("/rabbitTemplateSendDiy")
  public void rabbitTemplateSendDiy(@RequestParam(value = "exchange", required = false) String exchange,
                                    //路由键：队列的key
                                    @RequestParam(value = "routingKey", required = false) String routingKey,
                                    @RequestBody SysUserMpgtRsp sysUserMpgtRs) {
    String jsonStr = JSONUtil.toJsonStr(sysUserMpgtRs);
    rabbitTemplate.convertAndSend(exchange, routingKey, jsonStr);


  }







}
